﻿using zframe;
using System.IO;
using UnityEditor;
using UnityEditor.Build.Player;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;

namespace zframe.editor
{
    /// <summary>
    /// 把各个程序集的代码编译成dll文件 输出到指定目录
    /// </summary>
    public class BuildDLLTool
    {

        [MenuItem("zframe/BuildDlls/Wins")]
        static void BuildWinsDLLs()
        {
            BuildByTarget(BuildTarget.StandaloneWindows);
        }

        private static void BuildByTarget(BuildTarget target)
        {
            ScriptCompilationSettings settings = new ScriptCompilationSettings();
            settings.target = target;
            settings.group = BuildPipeline.GetBuildTargetGroup(target);

            string outDir = Application.dataPath;
            outDir = outDir.Substring(0, outDir.IndexOf("Assets"));
            outDir = Path.Combine(outDir, "ScriptDlls");
            if (!Directory.Exists(outDir))
            {
                Directory.CreateDirectory(outDir);
            }
            ScriptCompilationResult result = PlayerBuildInterface.CompilePlayerScripts(settings, outDir);
            ZLogger.Log("dll文件编译完成输出到：");
            foreach (var assm in result.assemblies)
            {
                ZLogger.LogFormat("{0}/{1}", outDir, assm);
            }
            EditorUtility.DisplayDialog("操作完成", $"DLL文件已经输出到{outDir}","朕知道了");
        }
    }
}
